(function(angular) {
	'use strict';
	angular.module('fs.waf').controller('cda.docTemplate.createVerifyController', createVerifyController);
	createVerifyController.$inject = [ '$scope', 'Constants', 'modalService', 'gridService', 'layoutConfig', 'validatorConfig', 'authService',
			'docTemplateService' ];
	function createVerifyController($scope, Constants, modalService, gridService, layoutConfig, validatorConfig, authService, docTemplateService) {
		var ctrl = this, params = $scope.$stateParams.params, errorMessage = {};
		var log = $scope.loggerFactory.getLogger("docTemplateCreateVerify");
		ctrl.layoutConfig = layoutConfig;
		ctrl.validatorConfig = validatorConfig;
		ctrl.docTemplate = {};
		ctrl.docTemplateOld = {};
		
		ctrl.init = function() {
			//init dTempParms
			ctrl.innerPreGridOptions = gridService.createInnerGridOptions($scope, ctrl.layoutConfig.formConfigs.docTempPreParms.tableConfigs.table, rowSelectionChanged);
			ctrl.innerPreGridOptions.data = 'vm.docTemplate.docTempPreParms';
			ctrl.innerPostGridOptions = gridService.createInnerGridOptions($scope, ctrl.layoutConfig.formConfigs.docTempPostParms.tableConfigs.table, rowSelectionChanged);
			ctrl.innerPostGridOptions.data = 'vm.docTemplate.docTempPostParms';
			ctrl.innerPreGridOptions.enableRowHeaderSelection = false;
			//postParms
			ctrl.innerPostGridOptions.enableRowHeaderSelection = false;

			function rowSelectionChanged(eventRow, selectedRows) {
				ctrl.selectedDTParmList = selectedRows;
			}
			// init docTemplate Parameter
			docTemplateService.initDataOptions($scope, ctrl);
			
			authService.createVerify(params).then(function(response) {
				if (response.data && response.data.model && response.data.task) {
					ctrl.docTemplate = response.data.model || params;
					ctrl.docTemplate.taskId = response.data.task.taskId;
					ctrl.docTemplate.docTempPreParms = ctrl.docTemplate.docTempPreParms || [];
					ctrl.docTemplate.docTempPostParms = ctrl.docTemplate.docTempPostParms || [];
					
				}
			});
		};

		ctrl.createVerifyApprove = function() {
			docTemplateService.setPrePostParm($scope, ctrl);
			modalService.openConfirmDialogs({
				'messages' : [ {msg : 'title.dialog.confirm.approve', args : [ctrl.docTemplate.primaryKey]} ],
				'btnCallback' : {
					'ok' : function() {
						authService.createVerifyApprove(ctrl.docTemplate).then(approveSuccessful, rejectFailed);
					}
				}
			});
		};

		ctrl.createVerifyReject = function() {
			docTemplateService.setPrePostParm($scope, ctrl);
			modalService.openPromptDialogs({
				'btnCallback' : {		
					'ok' : function(comments) {
						ctrl.docTemplate.comments = comments;
						authService.createVerifyReject(ctrl.docTemplate).then(rejectSuccessful, rejectFailed);
					}
				}
			});
		};

		function approveSuccessful(response) {
			modalService.openAcknowledgeDialogs({
				'messages' : [ 'title.dialog.successful.approve' ],
				'btnCallback' : {
					'close' : function() {
						$scope.go('cda.workflow.wfSetup.list');
					}
				},
				'redirections' : [ {
					'name' : 'title.infobar.workflow.list',
					'state' : 'cda.workflow.wfSetup.list',
					'params' : {}
				}, {
					'name' : 'title.infobar.doctemplate.list',
					'state' : 'cda.docTemplate.list',
					'params' : {}
				}]
			});
		}

		function approveFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}

		function rejectSuccessful(response) {
			modalService.openAcknowledgeDialogs({
				'messages' : [ 'title.dialog.successful.reject' ],
				'btnCallback' : {
					'close' : function() {
						$scope.go('cda.workflow.wfSetup.list');
					}
				},
				'redirections' : [ {
					'name' : 'title.infobar.workflow.list',
					'state' : 'cda.workflow.wfSetup.list',
					'params' : {}
				}, {
					'name' : 'title.infobar.doctemplate.list',
					'state' : 'cda.docTemplate.list',
					'params' : {}
				}]
			});
		}

		function rejectFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}
	}
})(angular);